
using Newtonsoft.Json;
using System;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Text;
using Hotel.UI.Facade;


namespace Hotel.UI.Common
{
    /// <summary>
    /// Json字符串操作辅助类
    /// </summary>
    public class JsonHelper<T>
    where T : class
    {
        public JsonHelper()
        {


        }

        /// <summary>
        /// 转换Json字符串到具体的对象
        /// </summary>
        /// <param name="url">返回Json数据的链接地址</param>
        /// <returns></returns>
        public static T ConvertJson(string url)
        {
            HttpHelper httpHelper = new HttpHelper()
            {
                ContentType = "application/json"
            };
            string html = httpHelper.GetHtml(url);
            VerifyErrorCode(html);
            return JsonConvert.DeserializeObject<T>(html);
        }

        /// <summary>
        /// 转换Json字符串到具体的对象
        /// </summary>
        /// <param name="url">返回Json数据的链接地址</param>
        /// <param name="postData">POST提交的数据</param>
        /// <returns></returns>
        public static BaseResultJson ConvertJson(string url, string postData, bool isPost = true)
        {
            BaseResultJson res = null;
            try
            {
                HttpHelper httpHelper = new HttpHelper()
                {
                    ContentType = "application/json"
                };
                var html = "";
                if (isPost)
                {
                    html = httpHelper.GetHtml(url, postData, isPost);
                }
                else
                {
                    httpHelper.ContentType = "application/x-www-form-urlencoded";
                   html = httpHelper.GetHtml(url, postData, isPost);
                  //  html = httpHelper.HttpGet(url, postData);
                }


                JsonHelper<BaseResultJson>.VerifyErrorCode(html);


                res = JsonConvert.DeserializeObject<BaseResultJson>(html);
                return res;
            }
            catch (System.Exception ex)
            {
                int ii = 999;
            }

            return new BaseResultJson() { code = 10086, info = "系统异常" };

        }

        /// <summary>
        /// 转换Json字符串到具体的对象
        /// </summary>
        /// <param name="url">返回Json数据的链接地址</param>
        /// <returns></returns>
        public static string ConvertString(string url)
        {
            HttpHelper httpHelper = new HttpHelper()
            {
                ContentType = "application/json"
            };
            string html = httpHelper.GetHtml(url);
            JsonHelper<T>.VerifyErrorCode(html);
            return JsonConvert.DeserializeObject<string>(html);
        }

        /// <summary>
        /// 转换Json字符串到具体的对象
        /// </summary>
        /// <param name="url">返回Json数据的链接地址</param>
        /// <param name="postData">POST提交的数据</param>
        /// <returns></returns>
        public static string ConvertString(string url, string postData)
        {
            HttpHelper httpHelper = new HttpHelper()
            {
                ContentType = "application/json"
            };
            string html = httpHelper.GetHtml(url, postData, true);
            VerifyErrorCode(html);
            string result = JsonConvert.DeserializeObject<string>(html);
            return result;
        }

        /// <summary>
        /// 提交文件并解析返回的结果
        /// </summary>
        /// <param name="url">提交文件数据的链接地址</param>
        /// <param name="file">文件地址</param>
        /// <param name="nvc">名称集合对象</param>
        /// <returns></returns>
        public static T PostFile(string url, string file, NameValueCollection nvc = null)
        {
            HttpHelper httpHelper = new HttpHelper();
            string[] strArrays = new string[] { file };
            string str = httpHelper.PostStream(url, strArrays, nvc, null, null);
            JsonHelper<T>.VerifyErrorCode(str);
            return JsonConvert.DeserializeObject<T>(str);
        }
        /// <summary>
        /// 检查返回的记录，如果返回没有错误，或者结果提示成功，则不抛出异常
        /// </summary>
        /// <param name="content">返回的结果</param>
        /// <returns></returns>
        private static bool VerifyErrorCode(string content)
        {
            if (content.Contains("code"))
            {
                BaseResultJson baseResultJson = JsonConvert.DeserializeObject<BaseResultJson>(content);
                //非成功操作才记录异常，因为有些操作是返回正常的结果（{"errcode": 0, "info": "ok"}）
                if ((baseResultJson == null ? false : !baseResultJson.success))
                {
                    string str = string.Format("请求发生错误！错误代码：{0}，说明：{1}", baseResultJson.code, baseResultJson.info);
                    LogTextHelper.Error(baseResultJson.ToJson());
                    //  throw new Exception(str);
                }
            }
            return true;
        }

    }
}